<!-- 移库申请 -->
<template>
<view style="height: 100vh;">
 <layout index="/pages/wms/index" layoutTitle="移库申请">
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="物料" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批号" prop="batchNo">
        <el-input
          v-model="queryParams.batchNo"
          placeholder="请输入批号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="源仓库" prop="sourceWarehouseId">
        <el-select
          v-model="queryParams.sourceWarehouseId"
          placeholder="请选择源仓库"
          clearable
          filterable
          @change="handleSourceWhChange"
        >
          <el-option
            v-for="dict in warehouseIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="parseInt(dict.dictValue)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="源库区" prop="sourceRegionId">
        <el-select
          v-model="queryParams.sourceRegionId"
          placeholder="请选择源库区"
          clearable
          filterable
          @change="handleSourceRegionChange"
        >
          <el-option
            v-for="dict in sourceRegionOpts"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="源库位" prop="sourceLocationId">
        <el-select
          v-model="queryParams.sourceLocationId"
          placeholder="请选择源库位"
          clearable
          filterable
        >
          <el-option
            v-for="dict in sourceLocationOpts"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="目标仓库" prop="targetWarehouseId">
        <el-select
          v-model="queryParams.targetWarehouseId"
          placeholder="请选择目标仓库"
          clearable
          filterable
          @change="handleTargetWhChange"
        >
          <el-option
            v-for="dict in warehouseIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="parseInt(dict.dictValue)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="目标库区" prop="targetRegionId">
        <el-select
          v-model="queryParams.targetRegionId"
          placeholder="请选择目标库区"
          clearable
          filterable
          @change="handleTargetRegionChange"
        >
          <el-option
            v-for="dict in targetRegionOpts"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="目标库位" prop="targetLocationId">
        <el-select
          v-model="queryParams.targetLocationId"
          placeholder="请选择目标库位"
          clearable
          filterable
        >
          <el-option
            v-for="dict in targetLocationOpts"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="移库时间">
        <el-date-picker
          v-model="daterangeOperateDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="执行人" prop="operateUser">
        <el-select
          v-model="queryParams.operateUser"
          placeholder="请选择执行人"
          clearable
          filterable
        >
          <el-option
            v-for="dict in operateUserOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['warehouse:warehouseMove:remove']"
        >
          删除
        </el-button>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <el-table
      v-loading="loading"
      :data="warehouseMoveList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="物料" align="center" prop="materialName" width="200" />
      <el-table-column label="源仓库" align="center" prop="sourceWarehouseName" width="200" />
      <el-table-column label="源库区" align="center" prop="sourceRegionName" width="200" />
      <el-table-column label="源库位" align="center" prop="sourceLocationName" width="200" />
      <el-table-column label="批号" align="center" prop="batchNo" width="200" />
      <el-table-column label="目标仓库" align="center" prop="targetWarehouseName" width="200" />
      <el-table-column label="目标库区" align="center" prop="targetRegionName" width="200" />
      <el-table-column label="目标库位" align="center" prop="targetLocationName" width="200" />
      <el-table-column label="移出数量" align="center" prop="moveQuantity" width="200" />
      <el-table-column label="移库时间" align="center" prop="operateDate" width="200">
        <template #default="scope">
          <span>{{ parseTime(scope.row.operateDate, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行人" align="center" prop="operateUser" :formatter="operateUserFormat" width="200" />
      <el-table-column label="移库原因" align="center" prop="reason" width="200" />
      <el-table-column label="备注" align="center" prop="remark" width="200" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['warehouse:warehouseMove:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
  </layout>
</view>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";

// 响应式状态
const loading = ref(true);
const warehouseMoveList = ref([]);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  materialName: null,
  sourceWarehouseId: null,
  sourceRegionId: null,
  sourceLocationId: null,
  batchNo: null,
  targetWarehouseId: null,
  targetRegionId: null,
  targetLocationId: null,
  operateDate: null,
  operateUser: null,
  status: null,
  orderByColumn: "operate_date",
  isAsc: "desc",
});
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const showSearch = ref(true);
const total = ref(0);
const daterangeOperateDate = ref([]);
const operateUserOptions = ref([]);
const warehouseIdOptions = ref([]);
const sourceRegionOpts = ref([]);
const sourceLocationOpts = ref([]);
const targetRegionOpts = ref([]);
const targetLocationOpts = ref([]);

// 模拟接口数据
const getList = () => {
  loading.value = true;
  setTimeout(() => {
    warehouseMoveList.value = [
      {
        id: 1,
        materialName: "物料A",
        sourceWarehouseName: "仓库1",
        sourceRegionName: "库区1",
        sourceLocationName: "库位1",
        batchNo: "B001",
        targetWarehouseName: "仓库2",
        targetRegionName: "库区2",
        targetLocationName: "库位2",
        moveQuantity: 100,
        operateDate: new Date(),
        operateUser: "张三",
        reason: "库存调整",
        remark: "无",
      },
      {
        id: 2,
        materialName: "物料B",
        sourceWarehouseName: "仓库1",
        sourceRegionName: "库区1",
        sourceLocationName: "库位1",
        batchNo: "B002",
        targetWarehouseName: "仓库2",
        targetRegionName: "库区2",
        targetLocationName: "库位2",
        moveQuantity: 200,
        operateDate: new Date(),
        operateUser: "李四",
        reason: "库存调整",
        remark: "无",
      },
    ];
    total.value = 2;
    loading.value = false;
  }, 1000);
};

onMounted(() => {
  getList();
});

// 模拟执行人字典翻译
const operateUserFormat = (row, column) => {
  return operateUserOptions.value.find((item) => item.dictValue === row.operateUser)?.dictLabel || row.operateUser;
};

// 搜索按钮操作
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

// 重置按钮操作
const resetQuery = () => {
  daterangeOperateDate.value = [];
  Object.assign(queryParams, {
    pageNum: 1,
    pageSize: 10,
    materialName: null,
    sourceWarehouseId: null,
    sourceRegionId: null,
    sourceLocationId: null,
    batchNo: null,
    targetWarehouseId: null,
    targetRegionId: null,
    targetLocationId: null,
    operateDate: null,
    operateUser: null,
    status: null,
  });
  handleQuery();
};

// 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};

// 删除按钮操作
const handleDelete = (row) => {
  const ids = row.id || ids.value;
  ElMessage.success("删除成功");
  getList();
};

// 导出按钮操作
const handleExport = () => {
  ElMessage.success("导出成功");
};
</script>

<style scoped>
.app-container {
  padding: 1rem;
}

@media (max-width: 768px) {
  .el-form-item {
    display: block;
    width: 100%;
  }

  .el-table {
    font-size: 14px;
  }

  .el-button {
    margin: 0.5rem 0;
  }
}
</style>